<?php
namespace app\admin\controller;

use think\Db;
use app\models\Config as ConfigModel;
use app\models\ConfigType as ConfigTypeModel;
use app\facade\Redis;

class System extends Base
{

    public function industrys()
    {
        $obj = Db::table("tb_industry");
        $list = $obj->paginate(10, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }

    public function industry_edit(){
        $id = $this->request->param('id');
        $row = Db::table("tb_industry")->where("id",$id)->find();
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($id > 0) {
                $bool = Db::table("tb_industry")->where('id',$id)->update($data);
            } else {
                $bool = Db::table("tb_industry")->insert($data);
            }
            return ['status' => 0, 'info' => '操作成功'];
        }
        return view('', ['data' => $row]);
    }

    public function industry_del()
    {
        $id = $this->request->post('id');
        Db::name('industry')->delete($id);
        return ['status' => "y", 'info' => '删除成功'];
    }

    public function config()
    {
        $obj = Db::table("tb_config")->order('sy type desc');
        $status = $this->request->param('status');
        $word = $this->request->param('word');
        if (!empty($word)){
            $obj->whereLike('key|desc', "%$word%");
        }
        if (!empty($status)){
            $obj->where('type', $status);
        }
        $list = $obj->paginate(10, false, ['query' => $this->request->param()]);
        $objType = new ConfigTypeModel;
        $types = $objType->select();
        return view('', ['list' => $list,'types' => $types,'type'=>$status]);
    }

    public function config_edit()
    {

        $model = new ConfigModel;
        $id = $this->request->param('id');
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data["sy"]=0;
            if ($id > 0) {
                $count=Db::table("tb_config")->where("key",$data['key'])->where('id','neq',$id)->count();
                if ($count>0){
                    return ['status' => 1, 'info' => '操作失败,key已存在'];
                }
                $bool = Db::table("tb_config")->where('id',$id)->update($data);
            } else {
                $count=Db::table("tb_config")->where("key",$data['key'])->count();
                if ($count>0){
                    return ['status' => 1, 'info' => '操作失败,key已存在'];
                }
                $bool = $model->save($data);
            }
            return ['status' => 0, 'info' => '操作成功'];
        } else {
            $objType = new ConfigTypeModel;
            $types = $objType->select();
            $row = $model->where('id', $id)->find();
            return view(null, ['data' => $row,'types'=>$types]);
        }
    }

    public function feed_config_redis()
    {
        $obj = new ConfigModel;
        $obj->order('type', 'asc');
        $list = $obj->select();
        foreach ($list as $key => $value){
            Redis::hset($value["type"],$value["key"],$value["name"]);
        }
        Db::table("tb_config")->where('sy','neq',1)->update(["sy"=>1]);
        return ['status' => 0, 'info' => '操作成功'];
    }

    public function region()
    {
        $model = new \app\models\Region;
        $list = $model->paginate(10, false, ['query' => $this->request->param()]);
        return view(null, ['list' => $list]);
    }

    public function region_edit()
    {
        $model = new \app\models\Region;
        $id = $this->request->param('id');
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if ($id > 0) {
                $bool = $model->save($data, ['id' => $id]);
            } else {
                $bool = $model->save($data);
            }
            return ['status' => 0, 'info' => '操作成功'];
        } else {
            $row = $model->where('id', $id)->find();
            return view(null, ['data' => $row]);
        }
    }

    public function region_del()
    {
        $id = $this->request->param('id');
        $model = \app\models\Region::where('id', $id)->delete();
        return ['status' => 0, 'info' => '操作成功'];
    }

    public function banner()
    {
        $list = Db::name('banner')->order('id', 'desc')->paginate(10, false, ['query' => $this->request->param()]);
        return view(null, ['list' => $list]);
    }

    public function banner_edit()
    {
        if ($this->request->isPost()) {
            $file = request()->file('image');
            $info = $file->validate(['size' => 856780, 'ext' => 'jpg,png,gif'])->move('uploads');
            if ($info) {
                $data = $this->request->post();
                Db::name('banner')->insert([
                    'title' => $data['title'],
                    'content' =>  'uploads/' . $info->getSaveName(),
                    'updated' => date('Y-m-d H:i:s'),
                    'created' => date('Y-m-d H:i:s'),
                ]);
                return ['status' => 0];
            } else {
                return ['status' => 1, 'info' => $file->getError()];
            }
        } else {
            return view();
        }
    }
    public function banner_del()
    {
        $id = $this->request->post('id');
        Db::name('banner')->delete($id);
        return ['status' => 0, 'info' => '删除成功'];
    }

    public function notice()
    {
        $list = Db::name('notice')->order('id', 'desc')->paginate(10, false, ['query' => $this->request->param()]);
        return view(null, ['list' => $list]);
    }

    public function notice_edit()
    {
        $id = $this->request->param('id');
        $data = $this->request->post();
        if ($this->request->isPost()) {
            if ($id > 0) {
                Db::name('notice')->where('id', $id)->update([
                    'title' => $data['title'],
                    'is_top' => $data['is_top'],
                    'content' => $data['content'],
                    'created' => $data['created'],
                ]);
            } else {
                Db::name('notice')->insert([
                    'title' => $data['title'],
                    'is_top' => $data['is_top'],
                    'content' => $data['content'],
                    'created' => $data['created'],
                ]);
            }
            return ['status' => 0];
        } else {
            return view(null, ['data' => Db::name('notice')->find(['id' => $id])]);
        }
    }

    public function notice_del()
    {
        $id = $this->request->post('id');
        Db::name('notice')->delete($id);
        return ['status' => 0, 'info' => '删除成功'];
    }

    public function contact_del()
    {
        $id = $this->request->post('id');
        Db::name('contact')->delete($id);
        return ['status' => 0, 'info' => '删除成功'];
    }

    public function testcontact()
    {
		 $str = file_get_contents('/www/wwwroot/ctzb/application/admin/controller/11.txt');//将整个文件内容读入到一个字符串中 
	   $str_encoding = mb_convert_encoding($str, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');//转换字符集（编码）
	   $arr = explode("\r\n", $str_encoding);//转换成数组 //去除值中的空格
	  foreach ($arr as &$row) { $row = trim($row); } 
	  foreach($arr as $key=>$val){
	
		  $allsddff=Db::table('tb_user')->where('username','=',$val)->field('id')->find();
		  Db::table('tb_user')->where('id',$allsddff['id'])->setField('status', 1);
		 unset($allsddff);
		  
	  }
	  exit();
	$allsddff=Db::table('tb_user')->where('level','=',0)->where('status','=',0)->field('id')->select();
	foreach($allsddff as $ks=>$vs){
		 Db::table('tb_user')->where('id',$vs['id'])->setField('status', 1);
		 
		}
       echo 22222;
	   exit();
    }
	
	 public function contact()
    {
        $list = Db::name('contact')->order('id', 'desc')->paginate(10, false, ['query' => $this->request->param()]);
        return view(null, ['list' => $list]);
    }

    public function contact_edit()
    {
        $id = $this->request->param('id');
        $data = $this->request->post();
        if ($this->request->isPost()) {
            $file = request()->file('image');
            $info = $file->validate(['size' => 856780, 'ext' => 'jpg,png,gif,jpeg'])->move('uploads');
            if ($info) {
                $data = $this->request->post();
                Db::name('contact')->insert([
                    'type' => $data['type'],
                    'number' => $data['number'],
                    'img' => 'uploads/' . $info->getSaveName(),
                ]);
                return ['status' => 0];
            } else {
                return ['status' => 1, 'info' => $file->getError()];
            }
        } else {
            return view(null, ['data' => Db::name('contact')->find(['id' => $id])]);
        }
    }
}

